//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ruoyi.web.controller.app;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.live.app.service.CacheService;
import com.ruoyi.live.app.service.OpenService;
import com.ruoyi.live.tool.commons.ICommons;
import com.ruoyi.live.tool.utils.Message;
import com.ruoyi.live.tool.utils.SystemInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import jakarta.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.util.*;

@Controller
@RequestMapping
public class OpenController {
    @Autowired
    OpenService service;
    @Autowired
    CacheService cacheService;

    @Autowired
    SystemInfo systemInfo;

    public OpenController() {
    }

    @RequestMapping({"/open/welcome"})
    public String welcome() {
        return "welcome";
    }

    @RequestMapping({"/g"})
    public ModelAndView g() {
        return (new ModelAndView("welcome")).addObject("tgUrl", this.cacheService.getParam("TG_1"));
    }

    @RequestMapping({"/g/{id}"})
    public ModelAndView wel(@PathVariable Integer id, HttpServletRequest request) {
        if (!SystemInfo.hasInitApplication) {
            this.initApplication(request);
        }

        return new ModelAndView("welcome_zz");
    }

    @RequestMapping({"/g1"})
    public String g1() {
        return "welcome";
    }

    @RequestMapping({"/open/welcomeTip"})
    public String welcomeTip() {
        return "welcome_layer";
    }

    @ResponseBody
    @RequestMapping({"/open/api/login"})
    public Message appLogin(String username, String password) {
        return this.service.appLogin(username, password);
    }

    @RequestMapping({"/open/live/index"})
    public ModelAndView liveIndex() {
        return new ModelAndView("/mapper");
    }

    @ResponseBody
    @RequestMapping({"/open/insert/body"})
    public Message insertBody(Integer count) {
        this.service.insertBody(count);
        return new Message(true, "");
    }

    @ResponseBody
    @RequestMapping({"/open/online"})
    public Message online() {
        return new Message(true, "");
    }

    @ResponseBody
    @RequestMapping({"/open/getIpPort"})
    public Message getIpPort() {
        Map<String, String> res = new HashMap();
        res.put("ip", this.cacheService.getParam("RACE_APP_IP"));
        res.put("port", this.cacheService.getParam("RACE_APP_PORT"));
        return new Message(true, JSON.toJSONString(res));
    }

    @RequestMapping({"/open/app"})
    public String app() {
        return "/app_download";
    }

    @RequestMapping({"/open/ztcapp"})
    public String ztcapp() {
        return "/ztcapp";
    }

    @RequestMapping({"/open/app_norecord"})
    public String app_norecord() {
        return "/app_download_norecord";
    }

    public Message sigleUpload(HttpServletRequest request) {
        try {
            List<MultipartFile> files = ((MultipartHttpServletRequest)request).getFiles("files");
            Iterator var3 = files.iterator();

            while(var3.hasNext()) {
                MultipartFile file = (MultipartFile)var3.next();
                String fileName = file.getOriginalFilename();
                if (ICommons.isNullOrEmpty(fileName)) {
                    throw new Exception("文件名不能为空");
                }

                if (fileName.indexOf("\\") != -1) {
                    fileName = fileName.substring(fileName.lastIndexOf("\\"));
                }

                String ext = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();
                if (ICommons.isNullOrEmpty(ext)) {
                    throw new Exception("扩展名不能为空");
                }

                if (!".jpg.png.bmp.jpeg.gif".contains(ext)) {
                    throw new Exception("请上传.jpg.png.bmp.jpeg.gif文件");
                }

                String filePath = request.getServletContext().getRealPath("/static/");
                File targetFile = new File(filePath);
                if (!targetFile.exists()) {
                    targetFile.mkdirs();
                }

                FileOutputStream out = null;
                String name = UUID.randomUUID().toString().replaceAll("-", "") + ext;

                try {
                    out = new FileOutputStream(filePath + name);
                    out.write(file.getBytes());
                } catch (Exception var20) {
                    var20.printStackTrace();
                    throw new Exception("上传失败");
                } finally {
                    try {
                        out.flush();
                        out.close();
                    } catch (Exception var19) {
                        var19.printStackTrace();
                    }

                }
            }

            return new Message(true, "");
        } catch (Exception var22) {
            var22.printStackTrace();
            return new Message(false, "");
        }
    }

    public void initApplication(HttpServletRequest request) {
        request.getServletContext().setAttribute("BASE_TITLE", systemInfo.getSoftName() + systemInfo.getSoftVersion());
        request.getServletContext().setAttribute("COPY_RIGHT", systemInfo.getCopyRight());
        request.getServletContext().setAttribute("LOGIN_LOGO", systemInfo.getComLoginLogo());
        request.getServletContext().setAttribute("INDEX_LOGO", systemInfo.getComLogo());
        SystemInfo.hasInitApplication = true;
    }

    @ResponseBody
    @RequestMapping({"/open/invoice"})
    public Message invoice() {
//        this.service.invoice();
        return new Message(true, "");
    }
}
